package com.zonhar.autofeed.common.device.rpi;

import com.pi4j.io.gpio.*;

/**
 * 驱动rpi模块工具
 */
public class RpiDeviceUtils {

    public static  long getHx711Weight(){
        GpioController gpio = GpioFactory.getInstance();

        Pin sckPin = RaspiPin.GPIO_25;

        Pin dtPin = RaspiPin.GPIO_23;

        GpioPinDigitalOutput sckGpio  = gpio.provisionDigitalOutputPin(sckPin, "SCK", PinState.LOW);

        GpioPinDigitalInput datGpio = gpio.provisionDigitalInputPin(dtPin, "DT",PinPullResistance.OFF);

        Hx711 hx711 =new Hx711(datGpio,sckGpio,10,1.0, GainFactor.GAIN_128);
        long weight =hx711.measure();
        //关掉控制器，还有引脚
        unprovisionPin(gpio,datGpio,sckGpio);

        return weight;
    }

    /**
     * 卸载控制器
     * @param gpio 控制器
     * @param gpioPin 引脚
     */
    private static void unprovisionPin(GpioController gpio, GpioPin... gpioPin){
      //  gpio.shutdown(); //关闭控制器
        //关掉使用的引脚
        gpio.unprovisionPin(gpioPin);
    }

}
